package com.yinhai.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.yinhai.cfg.AppConfig;
import com.yinhai.constant.Contact;

/**
 * 
 * @author Juyon 程序入口类
 */
public class LoadActivity extends Activity {
	private String text = "";
	private ImageButton pwd_1 = null;
	private ImageButton pwd_2 = null;
	private ImageButton pwd_3 = null;
	private ImageButton pwd_4 = null;
	private Context context;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.load);
		init();
		TextView load_back = (TextView) this.findViewById(R.id.load_back);
		TextView load_del = (TextView) this.findViewById(R.id.load_del);
		load_back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				android.os.Process.killProcess(android.os.Process.myPid()); // 获取PID
				System.exit(0); // 常规java、c#的标准退出法，返回值为0代表正常退出
			}

		});
		load_del.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (text != null && text.length() > 0) {
					text = text.substring(0, text.length() - 1);
					int length = text.length();
					if (length == 0) {
						((ImageButton) pwd_1).setImageDrawable(getResources()
								.getDrawable(R.drawable.dis_input));
					}
					if (length == 1) {
						((ImageButton) pwd_2).setImageDrawable(getResources()
								.getDrawable(R.drawable.dis_input));
					}
					if (length == 2) {
						((ImageButton) pwd_3).setImageDrawable(getResources()
								.getDrawable(R.drawable.dis_input));
					}
					if (length == 3) {
						((ImageButton) pwd_4).setImageDrawable(getResources()
								.getDrawable(R.drawable.dis_input));
					}
				}

			}

		});
	}

	private void valChange(String numChar) {
		// text = text + "0";
		if (text.length() < 4) {
			text = text + numChar;
		}
		int length = text.length();

		if (length == 1) {
			((ImageButton) pwd_1).setImageDrawable(getResources().getDrawable(
					R.drawable.input));
		}
		if (length == 2) {
			((ImageButton) pwd_2).setImageDrawable(getResources().getDrawable(
					R.drawable.input));
		}
		if (length == 3) {
			((ImageButton) pwd_3).setImageDrawable(getResources().getDrawable(
					R.drawable.input));
		}
		if (length == 4) {
			((ImageButton) pwd_4).setImageDrawable(getResources().getDrawable(
					R.drawable.input));
		}
		if (length == 4) {
			SharedPreferences preferences1 = LoadActivity.this
					.getSharedPreferences(AppConfig.LOGIN_CONFIG,
							Context.MODE_PRIVATE);
			String setuppwdstr1 = preferences1.getString(
					Contact.LOCAL_LOGIN_PWD, "");
			if (setuppwdstr1.equals(text)) {
				Intent mainIntent = new Intent(LoadActivity.this,
						MainActivity.class);
				startActivity(mainIntent);
				finish();
			} else {
				Toast.makeText(this, "密码错误！", Toast.LENGTH_SHORT).show();
			}

		}
	}

	private void init() {
		pwd_1 = (ImageButton) findViewById(R.id.pwd_1);
		pwd_2 = (ImageButton) findViewById(R.id.pwd_2);
		pwd_3 = (ImageButton) findViewById(R.id.pwd_3);
		pwd_4 = (ImageButton) findViewById(R.id.pwd_4);
		ImageButton pwd_value_0 = (ImageButton) findViewById(R.id.pwd_value_0);
		ImageButton pwd_value_1 = (ImageButton) findViewById(R.id.pwd_value_1);
		ImageButton pwd_value_2 = (ImageButton) findViewById(R.id.pwd_value_2);
		ImageButton pwd_value_3 = (ImageButton) findViewById(R.id.pwd_value_3);
		ImageButton pwd_value_4 = (ImageButton) findViewById(R.id.pwd_value_4);
		ImageButton pwd_value_5 = (ImageButton) findViewById(R.id.pwd_value_5);
		ImageButton pwd_value_6 = (ImageButton) findViewById(R.id.pwd_value_6);
		ImageButton pwd_value_7 = (ImageButton) findViewById(R.id.pwd_value_7);
		ImageButton pwd_value_8 = (ImageButton) findViewById(R.id.pwd_value_8);
		ImageButton pwd_value_9 = (ImageButton) findViewById(R.id.pwd_value_9);
		pwd_value_0.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("0");
			}

		});
		pwd_value_1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("1");
			}

		});
		pwd_value_2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("2");
			}

		});
		pwd_value_3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("3");
			}

		});
		pwd_value_4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("4");
			}

		});
		pwd_value_5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("5");

			}

		});
		pwd_value_6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("6");

			}

		});
		pwd_value_7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("7");

			}

		});
		pwd_value_8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("8");

			}

		});
		pwd_value_9.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				valChange("9");

			}

		});
	}
}
